using BlockOS.ChunkStorage;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

namespace BlockOS.Client
{
    public class HUDWindow : Window
    {
        [Header("Prefabs")]
        [SerializeField]
        private MobileControls mobileControls;
        [SerializeField]
        private PauseDialog pauseDialog;

        [Header("Bindings")]
        public Crosshair crosshair;

        [Space]
        [Header("Inputs")]
        [SerializeField]
        private InputActionMap hudMap;

        protected override void Awake()
        {
            hudMap["Back"].performed += _ => Back();
            hudMap["SwitchCompass"].performed += _ => crosshair.SwitchCompass();

            if (Settings.CONTROL_MODE == Settings.ControlMode.Touchscreen)
            {
                var mobi = Instantiate(mobileControls, transform);
                mobi.transform.SetAsFirstSibling();
                mobi.parent = this;
            }

            
        }

        public override void OnForeground()
        {
            hudMap.Enable();
        }

        public override void OnBackground()
        {
            hudMap.Disable();
        }

        public override void OnShow()
        {
            GameClient.Instance.EventBus.SetEventHandler(ClientEventId.PLAYER_LOOK, HandlePlayerLook);
        }

        public override void OnHide()
        {
            GameClient.Instance.EventBus.RemoveAllEventHanlder(ClientEventId.PLAYER_LOOK);
        }

        public void Back()
        {
            manager.PushWindow(pauseDialog, WindowManager.PushWindowMode.Addition);
        }

        private void HandlePlayerLook(GameEvent x)
        {
            var fwd = ((PlayerLookEvent)x).forward;
            crosshair.SetForward3D(fwd);
        }
    }
}
